/**
 *与后台交互需要修改的有：$(document).ready的window.delLink
 */
$(document).ready(function () {
    init();
});

function init() {
     currentSelectedIndex = -1;

    addSelectedOperator();
    bindClickOnUpdate();

    $("#apply").click(function() {

        var arrSlidesId = new Array();
        $("#slides-table tbody tr").each(function(index){
            var id = parseInt($(this).attr("id"));
            arrSlidesId[index] = id;
        });

        $("#slides-id-input").val(JSON.stringify(arrSlidesId));
        // alert("slides-id-input:" + $("#slides-id-input").val());
    });
    $("#moveLeft").click(function() {
        var $owlItems = $(".owl-item");
        var $trs = $("tbody tr");
        if (currentSelectedIndex > 0 && currentSelectedIndex<=$owlItems.length-1) {
        moveLeft($owlItems, $trs, currentSelectedIndex--);
        newOwlItems($owlItems);
        newTableRow($trs);
        addSelectedOperator();
        bindClickOnUpdate();
        }
    });

    $("#moveRight").click(function(){
        var $owlItems = $(".owl-item");
        var $trs = $("tbody tr");
        if(currentSelectedIndex >= 0 && currentSelectedIndex<$owlItems.length-1){
        moveRight($owlItems, $trs, currentSelectedIndex++);
        newOwlItems($owlItems);
        newTableRow($trs);
        addSelectedOperator();
        bindClickOnUpdate();
        }
    });


}

function addSelectedOperator(){
    var $link = $(".link");
    $link.each(function(index){
        $(this).click(function () {
            currentSelectedIndex = index;
            $link.removeClass("selected");
            $(this).addClass("selected");
        });
    });
}
//为owl-wrapper从新添加owl-item
function newOwlItems($owlItems){
    $(".owl-wrapper").empty();
    $(".owl-wrapper").append($owlItems);
}

//为table从新添加tr
function newTableRow($trs){
    $("tbody").empty();
    $("tbody").append($trs);
}

function moveLeft($owlItems, $trs, selectedIndex){
    if(selectedIndex<0){
        return;
    }else {
        var tmp = $owlItems[selectedIndex];
        $owlItems[selectedIndex] = $owlItems[selectedIndex - 1];
        $owlItems[selectedIndex - 1] = tmp;

        tmp = $trs[selectedIndex];
        $trs[selectedIndex] = $trs[selectedIndex-1];
        $trs[selectedIndex-1] = tmp;
    }
}

function moveRight($owlItems, $trs, selectedIndex){

    if(selectedIndex>=$owlItems.length-1){
        return;
    }else {
        var tmp = $owlItems[selectedIndex];
        $owlItems[selectedIndex] = $owlItems[selectedIndex + 1];
        $owlItems[selectedIndex + 1] = tmp;

        tmp = $trs[selectedIndex];
        $trs[selectedIndex] = $trs[selectedIndex+1];
        $trs[selectedIndex+1] = tmp;
    }
}
//点击编辑按钮，将id放到隐藏的input中
function bindClickOnUpdate(){
    $(".update").click(function(){
        //将幻灯片的id放到编辑模态框的隐藏input中
        var slideId = $(this).data("slide-id");
        var imgId = $(this).data("img-id");
        $("#edit-id-input").val(slideId);

        //设置编辑模态框的select的value1
        $("#edit-slide select").val(imgId);
    });
}